package com.kye.express.business.personal_setting.modify_name;

import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.view.ErrorView;
import com.kye.express.contract.base.view.IView;
import com.kye.express.contract.base.view.LoadingView;
import com.kye.model.OpenApiResultObserver;
import com.kye.model.UserSession;
import com.kye.model.base.RetrofitManager;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.user.User;
import com.kye.model.bean.user.request.RequestEditPersonalSetting;
import com.kye.model.bean.user.request.RequestGetUser;
import com.kye.model.service.UserService;

import io.reactivex.SingleSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

public interface ModifyNameOrCompanyContract {
    interface ModifyNameOrCompanyView extends IView, LoadingView, ErrorView {

        void onUser(User user);

        void onSuccess();
    }

    class ModifyNameOrCompanyPresenter extends BasePresenter<ModifyNameOrCompanyView> {

        public void editPersonalSetting(final User user, boolean type, String value) {
            RetrofitManager.getInstance()
                    .createOpenApiService(UserService.class)
                    .editPersonalSetting(new RequestEditPersonalSetting(user.getPhone(), type ? value : null, null, type ? null : value, null))
                    .flatMap(new Function<OpenApiResult<Object>, SingleSource<OpenApiResult<User>>>() {
                        @Override
                        public SingleSource<OpenApiResult<User>> apply(OpenApiResult<Object> objectOpenApiResult) throws Exception {
                            return RetrofitManager.getInstance()
                                    .createOpenApiService(UserService.class)
                                    .getUser(new RequestGetUser(user.getPhone()));
                        }
                    })
                    .doOnSuccess(new Consumer<OpenApiResult<User>>() {
                        @Override
                        public void accept(OpenApiResult<User> userOpenApiResult) throws Exception {
                            User newUser = userOpenApiResult.getData();
                            if (newUser == null) return;
                            newUser.setToken(user.getToken());
                            newUser.setRefreshToken(user.getRefreshToken());

                            UserSession.getInstance().setUser(newUser);
                        }
                    })
                    .compose(getView().<OpenApiResult<User>>bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<User>() {
                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onError();
                        }

                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onSuccess(OpenApiResult<User> result, User user) {
                            getView().onSuccess();
                        }

                        @Override
                        public void onApiError(OpenApiResult<User> result, int code, String msg) {
                            getView().onError(msg);
                        }

                        @Override
                        public void onLoaded() {
                            getView().onLoaded();
                        }
                    });
        }

        public void getUser() {
            UserSession.getInstance()
                    .getUserForRx()
                    .compose(getView().<User>bindToLifecycle())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<User>() {
                        @Override
                        public void accept(User user) throws Exception {
                            getView().onUser(user);
                        }
                    });
        }
    }
}
